package com.oasystem.service;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.oasystem.dao.jdbc.OperatorDAO;
import com.oasystem.dao.jdbc.PermissionDAO;
import com.oasystem.domain.Operator;
import com.oasystem.domain.PagingBean;

@Transactional(readOnly = true)
public class OperatorService {
	private OperatorDAO operatorDao;
	private PermissionDAO permissionDAO;

	public void setPermissionDAO(PermissionDAO permissionDAO) {
		this.permissionDAO = permissionDAO;
	}

	public void setOperatorDao(OperatorDAO operatorDao) {
		this.operatorDao = operatorDao;
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void insertOperator(Operator operator) {
		int operatorID = operatorDao.insertOperator(operator);
		operator.setId(operatorID);
		permissionDAO.insertUserPermission(operator);
	}
	
	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void updateOperator(Operator operator) {
		operatorDao.updateOperator(operator);
		permissionDAO.deleteUserPermission(operator);
		permissionDAO.insertUserPermission(operator);
	}
	
	public List<Operator> getPagingOperators(PagingBean<Operator> pagingBean){
		return operatorDao.getPagingOperators(pagingBean);
	}

	public List<Operator> getAllOperatorsWithoutPermission(){
		return operatorDao.getAllOperatorsWithoutPermission();
	}
	
	public void loadOperator(Operator operator) {
		operatorDao.load(operator);
		permissionDAO.loadByOperatorID(operator); 
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void deleteOperator(Operator operator) {
		operatorDao.deleteOperator(operator);
		permissionDAO.deleteUserPermission(operator);
		
	}

	public boolean isLegal(Operator operator) {
		return operatorDao.isLegal(operator);
	}

	public boolean isUserNameExist(String userName) {
		return operatorDao.isUserNameExist(userName);
	}
}
